import com.mochimedia.ui.*;
import mx.transitions.easing.None;

/**
* ...
* @author $(DefaultUser)
*/

class com.mochimedia.ui.Clip extends Component {
    
    public static var SCALEMODE_NOSCALE:Number = 1;
    public static var SCALEMODE_CENTER:Number = 2;
    public static var SCALEMODE_FILL:Number = 3;
    public static var SCALEMODE_FIT:Number = 4;
    public static var SCALEMODE_STRETCH:Number = 5;
    
    public static var EMBED_REMOTE:Number = 1;
    public static var EMBED_LOCAL:Number = 2;
    public static var EMBED_SMART:Number = 3;
    
    public static var baseURL:String = "";
    
    public var value:String = "";
    
    public static var clipsLoading:Number = 0;
    
    private var _type:String = "clip";
    
    private var _scaleMode:Number = 1;
    
    private var _url:String = "";
    
    private var _linkURL:String = "";
    
    private var _newWindow:Boolean = false;
    
    private var _alt:String = "";
    public function get alt ():String { return _alt; }
    public function set alt (value:String):Void { _alt = value; }
    
    private var _embedded:Number = EMBED_SMART;
    
    private var _loaderclip:MovieClip;
    private var _loader:MovieClipLoader;
    
    private var _btn:MovieClip;
    private var _clickable:Boolean = false;
    
    private var _loading:Boolean = false;
    
    private var _loaded:Boolean = false;
    public function get loaded():Boolean { return _loaded; }
    
    private var _debugmode:Boolean = false;
	
	public function get loadedClip():MovieClip { return _loaderclip; }

    
    //
    //
    public function Clip(container:MovieClip, url:String, embedded:Number, width:Number, height:Number, scaleMode:Number, linkURL:String, newWindow:Boolean, altTag:String, position:Position, style:Style) {
        
        init (container, url, embedded, width, height, scaleMode, linkURL, newWindow, altTag, position, style);
        
        if (_container != null) create();
        
    }
    
    //
    //
    private function init (container:MovieClip, url:String, embedded:Number, width:Number, height:Number, scaleMode:Number, linkURL:String, newWindow:Boolean, altTag:String, position:Position, style:Style):Void {
        
        super.init(container, position, style);
        
        _url = url;
        _embedded = embedded;
        _width = width;
        _height = height;
        _scaleMode = (!isNaN(scaleMode) && _width > 0 && _height > 0) ? scaleMode : SCALEMODE_NOSCALE;
        _linkURL = (linkURL.length > 0) ? linkURL : "";
        _newWindow = (newWindow == true);

        if (altTag.length > 0) _alt = altTag;
        
    }
    
    //
    //
    public function create ():Void {
        
        super.create();
        
        if (_url != undefined && _url.length > 0) {
            if( _embedded == EMBED_SMART || _embedded == EMBED_LOCAL )
            {
                var libName:String = _url.split("/").join("-");
    			_loaderclip = _mc.attachMovie(libName, libName + _mc.getNextHighestDepth(), _mc.getNextHighestDepth());
            }
                        
            if( _embedded == EMBED_REMOTE || ( _embedded == EMBED_SMART && _loaderclip == undefined ) )
            {                
				_loaderclip = _mc.createEmptyMovieClip("loaderclip", _mc.getNextHighestDepth());
				load();
            }
            else
            {
                onLoadInit(_loaderclip);
			}
        }
        
    }
    
    //
    //
    function rollover ():Void {

        if (_alt.length > 0) Tagtip.showTag(_alt);

    }
    
    //
    //
    function rollout ():Void {
        
        Tagtip.hideTag();
        
    }
    
    //
    //
    public function addEventListener (eventType:String, callbackObject:Object, callbackMethod:Function):Void {
        
        super.addEventListener(eventType, callbackObject, callbackMethod);
        
        if (eventType == EVENT_CLICK) {
            _clickable = true;
            _btn.useHandCursor = true;
        }
        
    }
    
    //
    //
    private function load ():Void {
        
        if (!_loading && !_loaded) {
            
            clipsLoading++;
            
            _loader = new MovieClipLoader();
            _loader.addListener(this);
            
            if (baseURL.length > 0 && baseURL.charAt(baseURL.length - 1) != "/") baseURL += "/";
            _loader.loadClip(baseURL + _url, _loaderclip);
            
            _loading = true;
        
        }
        
    }
    
    //
    //
    function onLoadStart (target_mc:MovieClip):Void {
        
        debug("Load start on movie clip = " + target_mc);

    }
    
    //
    //
    function onLoadProgress (target_mc:MovieClip, loadedBytes:Number, totalBytes:Number):Void {
        
        debug("Load progress on movie clip = " + target_mc + ", " + loadedBytes + " of " + totalBytes);

    }
    
    //
    //
    function onLoadComplete (target_mc:MovieClip):Void {
        debug("Your load is done on movie clip = "+target_mc);
        clipsLoading--;
        
        _loading = false;
        _loaded = true;

    }
    
    //
    //
    function onLoadInit (target_mc:MovieClip):Void {

        if (target_mc._width == 0) return;

        debug("Movie clip = " + target_mc + " is now initialized.");

        _btn = _mc.createEmptyMovieClip("btn", _mc.getNextHighestDepth());
        DrawingMethods.rect(_btn, false, 0, 0, _width, _height, 0x000000, 0.2);
        _btn._alpha = 0;
        connectButton(_btn);
            
        if (_alt.length > 0) {
            
            addEventListener(EVENT_ROLLOVER, this, rollover);
            addEventListener(EVENT_ROLLOUT, this, rollout);
             
        }
        
        if (_linkURL.length > 0) addEventListener(EVENT_CLICK, this, launchURL);
        else _btn.useHandCursor = false;
    
        target_mc._xscale = target_mc._yscale = 99.8;
        
        var targetAspect:Number = target_mc._width / target_mc._height;
        var clipAspect:Number = _width / _height;
        
        switch (_scaleMode) {
            
            case SCALEMODE_NOSCALE:
                break;
          
            case SCALEMODE_FILL:
                if (targetAspect > clipAspect) {
                    target_mc._height = _height;
                    target_mc._width = target_mc._height * targetAspect;
                } else {
                    target_mc._width = width;
                    target_mc._height = target_mc._width / targetAspect;
                }
                break;
                
            case SCALEMODE_FIT:
                if (targetAspect < clipAspect) {
                    target_mc._height = _height;
                    target_mc._width = target_mc._height * targetAspect;
                } else {
                    target_mc._width = width;
                    target_mc._height = target_mc._width / targetAspect;
                }
                
            case SCALEMODE_CENTER:
                if (target_mc._width > 0 && target_mc._width < _width) target_mc._x = Math.floor((_width - target_mc._width) * 0.5);
                if (target_mc._height > 0 && target_mc._height < _height) target_mc._y = Math.floor((_height - target_mc._height) * 0.5);
                break;
                
            case SCALEMODE_STRETCH:
                target_mc._width = _width;
                target_mc._height = _height;
     
        }
        
        if (_scaleMode == SCALEMODE_FILL) {
            
            var mask:MovieClip = _mc.createEmptyMovieClip("_mask", _mc.getNextHighestDepth());
            DrawingMethods.rect(mask, false, 0, 0, _width, _height, _style.backgroundColor, 0);

            target_mc.setMask(mask);
            
        }

    }
    
    //
    //
    function launchURL ():Void {
        
        if (_newWindow) getURL(_linkURL, "_blank");
        else getURL(_linkURL);
        
    }
    
    //
    //
    function onLoadError (target_mc:MovieClip, errorCode:String):Void {
        
        debug("ERROR: Load failed on clip '" + target_mc + "': " + errorCode);

    }
    
    //
    //
    function debug (msg:String):Void {
        
        if (_debugmode) trace(msg);
        
    }
    
}